set -e
set -o pipefail

if [ -z "${MIN_DETACHMENT_FORCE}" ]; then MIN_DETACHMENT_FORCE=200.0; fi
if [ -z "${MIN_INTERPEAKS}" ]; then MIN_INTERPEAKS=3; fi
if [ -z "${CL_START_NM}" ]; then CL_START_NM=15.0; fi
if [ -z "${CL_END_NM}" ]; then CL_END_NM=23.0; fi
if [ -z "${INPUT_PATH_FILENAME}" ]; then INPUT_PATH_FILENAME=filtered_True_${MIN_DETACHMENT_FORCE}_${MIN_INTERPEAKS}_${CL_START_NM}_${CL_END_NM}.curveStretchPlaylist; fi

if [ -z "${FILE_FORMAT}" ]; then FILE_FORMAT="nanoscope"; fi
if [ -z "${TEMPERATURE}" ]; then TEMPERATURE=301.15; fi
if [ -z "${TRACES_PATTERN}" ]; then TRACES_PATTERN="glob:**"; fi
if [ -z "${WLC_HIGH_PN}" ]; then WLC_HIGH_PN=500.0; fi
if [ -z "${EPS2PDF}" ]; then EPS2PDF="ps2pdf"; fi
if [ -z "${MIN_X_AXIS}" ]; then MIN_X_AXIS=1; fi
if [ -z "${MAX_X_AXIS}" ]; then MAX_X_AXIS=-1; fi
if [ -z "${MIN_Y_AXIS}" ]; then MIN_Y_AXIS=1; fi
if [ -z "${MAX_Y_AXIS}" ]; then MAX_Y_AXIS=-1; fi
if [ -z "${WLC_SAMPLES}" ]; then WLC_SAMPLES=100; fi
if [ -z "${X_AXIS}" ]; then X_AXIS="piezo"; fi
if [ -z "${LINE_WIDTH}" ]; then LINE_WIDTH="1.0"; fi

# BATCH_CLASSPATH for debugging: mvn dependency:build-classpath plus target/classes
if [ -z "${BATCH_CLASSPATH}" ]; then BATCH_CLASSPATH="target/refolding-core-0.1-jar-with-dependencies.jar"; fi

fixedDeflectionSensitivityNmPerV=$DEFL_SENS; fixedSpringConstantNPerM=$K; for d in $DIRS; do p=$(dirname ${d}); b=$(basename ${d})${fixedDeflectionSensitivityNmPerV//*/_${fixedDeflectionSensitivityNmPerV}}${fixedSpringConstantNPerM//*/_${fixedSpringConstantNPerM}}; cd ~/work/rcs/refolding-read-only/core && nice -n 10 java -ea -cp ${BATCH_CLASSPATH} -Djava.awt.headless=true it.unibo.refolding.pico.RetractionTraceExporter --fileFormat=${FILE_FORMAT} --contactPointAndSlopeDir=${p}/analysis/${b}_analysis/${b}_cps --junctionPointsDir=${p}/analysis/${b}_analysis/${b}_jp --dataDir= --fixedDeflectionSensitivityNmPerV=${fixedDeflectionSensitivityNmPerV} --fixedSpringConstantNPerM=${fixedSpringConstantNPerM} --inputPathList=${p}/analysis/${b}_analysis/${INPUT_PATH_FILENAME} --wildcardPattern=${TRACES_PATTERN} --tracesPath=${p}/analysis/${b}_analysis/${b}_traces.repr && cd ~/work/rcs/refolding-read-only/core/src/main/python && rm -fr ${p}/analysis/${b}_analysis/${b}_eps && python2.7 retraction_traces.py --retraction_playlist=${p}/analysis/${b}_analysis/${b}.repr --traces_repr_path=${p}/analysis/${b}_analysis/${b}_traces.repr --temperature_K=${TEMPERATURE} --p_nm=0.35 --eps_dir=${p}/analysis/${b}_analysis/${b}_eps --wlc_high_pn=${WLC_HIGH_PN} --min_x_axis=${MIN_X_AXIS} --max_x_axis=${MAX_X_AXIS} --min_y_axis=${MIN_Y_AXIS} --max_y_axis=${MAX_Y_AXIS} --wlc_samples=${WLC_SAMPLES} --x_axis=${X_AXIS} && (find ${p}/analysis/${b}_analysis/${b}_eps -name \*.eps | while read f; do (cd $(dirname ${f}) && ${EPS2PDF} $f); done); done
